package com.wandian.cqcsk.mvvm.mine.everydayenglish;

import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.ShareDialogBottom;
import com.mylib.libcore.mediaplayer.ManagedMediaPlayer;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.tools.ShareTools;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.QRCodeUtil;
import com.mylib.libcore.utils.StringUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.ViewUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.databinding.ActivityEveryDayEnglishBinding;

import java.io.IOException;

import static com.mylib.libcore.config.AppSPKeyConfig.INVITE_CODE_RUL;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description EveryDayEnglishActivity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_EVERYDAY_ENGLISH)
public class EveryDayEnglishActivity extends BaseMVVMActivity<EveryDayEnglishViewModel> implements ShareDialogBottom.CommonDialogBtnClink{
    private ActivityEveryDayEnglishBinding dataBinding;
    private ManagedMediaPlayer mMediaPlayer;
    private ShareDialogBottom shareDialogBottom;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_every_day_english);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        mViewModel.getEnglishByDay();

    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("每日英语");
        mViewModel.englishBean.observe(this,bean->{
            if(!UIUtils.isEmpty(bean)){
                GlideUtil.into(this,bean.getImgurl(),dataBinding.ivEnglishBg);
                dataBinding.tvEnglish.setText(bean.getContent()+"");
                dataBinding.tvEnglishFy.setText( bean.getNote()+"");
            }
        });

    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });

        //复制
        dataBinding.ivEnglishCopy.setOnClickListener(v -> {
            StringUtil.copyToClip(this, dataBinding.tvEnglish.getText()+"\n"+ dataBinding.tvEnglishFy.getText());
        });
        //分享
        dataBinding.ivEnglishShare.setOnClickListener(v -> {
            if(UIUtils.isFastClick())return;
            showShareDialog();
        });
        //播放
        dataBinding.ivEnglishPlay.setOnClickListener(v -> {
            playMusic();
        });
    }


    //展示分享弹框
    private void showShareDialog(){
        try {
            shareDialogBottom = new ShareDialogBottom(getContext(),this::btnClink);
            shareDialogBottom.showAtLocation(dataBinding.llRoot, Gravity.BOTTOM, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //播放
    private void playMusic(){
        if(UIUtils.isEmpty(mViewModel.english_audio))return;
        if(mMediaPlayer == null) {
            mMediaPlayer = new ManagedMediaPlayer();
        }else {
            mMediaPlayer.reset();
        }
        try {
            mMediaPlayer.setDataSource(mViewModel.english_audio);//进入初始态
            mMediaPlayer.prepareAsync();//异步准备
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    mMediaPlayer.start(); // 准备好了再播放
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void btnClink(String text, String type, String id) {
        String invite_url = AppContext.getInstance().getAppPreferenceForever().getKeyValue(INVITE_CODE_RUL)+"";
        if(!UIUtils.isEmpty(invite_url)){
            // Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.app_logo);
            final Bitmap er_bitmap = QRCodeUtil.createQRImage(invite_url, 110, 110, null, null);
            if(!UIUtils.isEmpty(er_bitmap)){
                dataBinding.ivErCode.setImageBitmap(er_bitmap);
            }
        }
        Bitmap share_bitmap = ViewUtils.toBitmap(dataBinding.llShare);
        if(!UIUtils.isEmpty(share_bitmap)){
            dataBinding.ivErCode.setImageResource(com.wandian.notepad.R.color.transparent);
            switch (type){
                case "微信":
                    ShareTools.shareWxPic(getContext(),share_bitmap,0);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "朋友圈":
                    ShareTools.shareWxPic(getContext(),share_bitmap,1);// 0：分享到好友  1：分享到朋友圈
                    break;
                case "QQ":
                    ToastUtil.show("分享QQ");
                    break;
                case "复制":
                    ToastUtil.show("已复制");
                    break;
            }

        }
    }
}
